package main

import (
  "pgi"
  "os"
  "tnet"
  "encoding/json"
  )

type EchoRequest struct {
  Str string
  Blah int32
}

type EchoResponse struct {
  Name string
  Id int32
}

func EchoHandler(request EchoRequest) EchoResponse {
  response := EchoResponse{"hi", request.Blah}
  return response
}

// Eventually this will be generated, and imported from another package.
func MyDispatchFunc(app_ctx interface{}, route string, app_request_str []byte) (app_response_str []byte) {
  if (route == "Echo") {
    tnet.Log("app_request %q", app_request_str)
    req := EchoRequest{}
    err := json.Unmarshal(app_request_str, &req)
    if err != nil {
      tnet.Log("Error in JSON: %s", err)
    }
    app_response := EchoHandler(req)

    tnet.Log("deserialized app erquest %s", req)
    tnet.Log("Str: %s  Blah: %s", req.Str, req.Blah)

    app_response_str, err = json.Marshal(app_response)

  } else {
    tnet.Log("Unknown route")
    os.Exit(1)
  }
  return
}

func main() {
  // nil for now
  var app_ctx interface{} = nil
  pgi.ServeDefault(MyDispatchFunc, app_ctx)
}
